/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import workers.statements.beans.CCStatementCSV;

/**
 *
 * @author bsmith
 */
@Entity
@Table(name = "statement")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Statement.findAll", query = "SELECT s FROM Statement s"),
    @NamedQuery(name = "Statement.findByDate", query = "SELECT s FROM Statement s WHERE s.date = :date"),
    @NamedQuery(name = "Statement.findEarliestDates", query = "SELECT MIN(s.date) FROM Statement s"),//SELECT s FROM Statement s WHERE s.date = :date"), //date = (SELECT MIN(date) FROM s)"),
    @NamedQuery(name = "Statement.findById", query = "SELECT s FROM Statement s WHERE s.id = :id"),
    @NamedQuery(name = "Statement.findByType", query = "SELECT s FROM Statement s WHERE s.type = :type"),
    @NamedQuery(name = "Statement.findByAmount", query = "SELECT s FROM Statement s WHERE s.amount = :amount"),
    @NamedQuery(name = "Statement.findByBalance", query = "SELECT s FROM Statement s WHERE s.balance = :balance"),
    @NamedQuery(name = "Statement.findByMerchantTxt", query = "SELECT s FROM Statement s WHERE s.merchantTxt = :merchantTxt"),
    @NamedQuery(name = "Statement.findByMerchant", query = "SELECT s FROM Statement s WHERE s.merchant.id = :merchant"),
    @NamedQuery(name = "Statement.findByCategory", query = "SELECT s FROM Statement s WHERE s.category.id = :category"),
    @NamedQuery(name = "Statement.findByAllFields", query = "SELECT s FROM Statement s WHERE s.type = :type and s.amount = :amount and s.balance = :balance and s.date = :date and s.merchantTxt = :merchantTxt")
})
public class Statement implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String dateFormatString = "dd/MM/yy";
    private static final DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 128)
    @Column(name = "type")
    private String type;
    @Basic(optional = false)
    @Column(name = "amount")
    private BigDecimal amount;
    @Basic(optional = false)
    @Column(name = "balance")
    private BigDecimal balance;
    @Basic(optional = false)
    @Size(min = 1, max = 128)
    @Column(name = "merchanttxt")
    private String merchantTxt;
    @JoinColumn(name = "merchant", referencedColumnName = "id")
    @ManyToOne
    private Merchants merchant;
    @JoinColumn(name = "category", referencedColumnName = "id")
    @ManyToOne
    private Categories category;
    @Basic(optional = false)
    @NotNull
    // all transactions within the month that is from statementStartMonthDay to statementStartMonthDay + Month - 1 day all have statementNumberDate thats the same
    @Column(name = "statementNumberDate")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date statementNumberDate;
    @Transient
    private int statementNumber;

    public Statement() {
    }

    public Statement(Long id) {
        this.id = id;
    }

    public Statement(Long id, Date date, String type, BigDecimal amount, BigDecimal balance, String merchantTxt) {
        this.id = id;
        this.date = date;
        this.type = type;
        this.amount = amount;
        this.balance = balance;
        this.merchantTxt = merchantTxt;
    }

    public Statement(CCStatementCSV ccStatementCSV, Merchants merchant, Categories category) throws ParseException {
        setDate(ccStatementCSV.getDate());
        setType(ccStatementCSV.getType());
        setAmount(ccStatementCSV.getAmount());
        setBalance(ccStatementCSV.getBalance());
        setMerchantTxt(ccStatementCSV.getMerchant());
        setMerchant(merchant);
        setCategory(category);
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public static Date formatDate(String datestr) throws ParseException {
        Date d = dateFormat.parse(datestr);
        return d;
    }

    public void setDate(String datestr) throws ParseException {
        this.date = formatDate(datestr);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public static BigDecimal formatAmount(String amountStr) {
        if (amountStr == null || amountStr.length() == 0) {
            return null;
        }

        return new BigDecimal(amountStr);
    }

    public void setAmount(String amountStr) {
        this.amount = formatAmount(amountStr);
    }

    public BigDecimal getBalance() {
        return balance;
    }

    /**
     * Separate method so can call when building queries that use a :balance
     * @param balanceStr
     * @return 
     */
    public static BigDecimal formatBalance(String balanceStr) {
        if (balanceStr == null || balanceStr.length() == 0) {
            return null;
        }

        return new BigDecimal(balanceStr);
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public void setBalance(String balanceStr) {
        this.balance = formatBalance(balanceStr);
    }

    public String getMerchantTxt() {
        return merchantTxt;
    }

    public void setMerchantTxt(String merchantTxt) {
        this.merchantTxt = merchantTxt;
    }

    public Merchants getMerchant() {
        return merchant;
    }

    public void setMerchant(Merchants merchant) {
        this.merchant = merchant;
    }

    public Categories getCategory() {
        return category;
    }

    public void setCategory(Categories category) {
        this.category = category;
    }

    public Date getStatementNumberDate() {
        return statementNumberDate;
    }

     public void setStatementNumberDate(Date statementNumberDate) {
        this.statementNumberDate = statementNumberDate;
    }

    public int getStatementNumber() {
        return statementNumber;
    }

    public void setStatementNumber(int statementNumber) {
        this.statementNumber = statementNumber;
    }

    // ---------------------------------------------------------
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Statement)) {
            return false;
        }
        Statement other = (Statement) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Statement[ id=" + id + " ] - amount:" + getAmount() + ", merchant:" + getMerchant().getId() + ", cat:" + getCategory().getId();
    }
}
